#define _CRT_SECURE_NO_WARNINGS 1
#include<set>
#include<iostream>
#include<map>
#include<string>
using namespace std;

void Print(set<int>& s)
{
	for (auto& e : s)
	{
		cout << e << ' ';
	}
	cout << endl;
}

void test1_set()
{
	set<int>s_empty;						//empty
	set<int>s = { 1,2,3,4,5,6,2,3,1,2,3 };	//去重
	cout << "s:";
	Print(s);
	set<int>s2(s);							//copy
	cout << "s2:";
	Print(s2);
	int array[] = { 1,2,3,4,5,6 };
	set<int>s3(array, array + sizeof(array) / sizeof(int));		//range
	cout << "s3:";
	Print(s3);
	cout << "s4:";
	set<int>s4(s.begin(), s.end());			//iterator
	Print(s4);
}

void test2_set()
{
	set<int>s = { 1,2,3,4,5 };
	cout << *(s.begin()) << endl;
	cout << *(--s.end()) << endl;
	set<int>::iterator it = s.begin();
	while (it != s.end())
	{
		cout<<*it<<' ';
		++it;
	}
}

void test3_set()
{
	set<int>s ;
	set<int>::iterator it;
	pair<set<int>::iterator, bool> ret;
	for (int i = 1; i < 5; i++)
	{
		s.insert(i * 10);
	}
	Print(s);
	ret = s.insert(20);			//已经存在，返回<iterator,false>
	if (ret.second == false)
		it = ret.first;
	s.insert(it, 21);
	s.insert(it, 22);
	s.insert(it, 23);
	s.insert(it, 24);
	s.insert(it, 25);
	Print(s);
}

void test4_set()
{
	set<int>s = { 10,20,30,40,50 };
	set<int>s2(s);
	s.erase(20);
	Print(s);
	set<int>::iterator it = s.begin();
	while(it!=s.end())
	{
		it = s.erase(it);
		Print(s);
	}
	Print(s2);
	set<int>::iterator itlow = s2.lower_bound(10);			//迭代器指向val>=10的元素，即10
	set<int>::iterator itupp = s2.upper_bound(40);			//迭代器指向val>40的元素，即50
	s2.erase(itlow, itupp);						//迭代器区间,erase是左闭右开
	Print(s2);
}


void test_multiset()
{
	int array[] = { 0,0,0,2,1,3,4,5 };

	// 注意：multiset在底层实际存储的是<int, int>的键值对
	multiset<int> s(array, array + sizeof(array) / sizeof(array[0]));
	s.erase(0);
	for (auto& e : s)
		cout << e << " ";
	cout << endl;
}

void test_map()
{
	map<string, int>m;
	m.insert(make_pair("a", 1));
	m.insert(make_pair("b", 2));
	m.insert(make_pair("d",3));
	for (auto& e : m)
	{
		cout << e.first << "->" << e.second << " " << endl;
	}
	cout << "------------------------------------" << endl;
	m["a"] = 2;
	m["b"] = 1;
	m["c"] = 3;
	for (auto& e : m)
	{
		cout << e.first << "->" << e.second << " "<<endl;
	}
}

int main()
{
	//test4_set();
	//test_multiset();
	test_map();
	return 0;
}